package com.tr.rentcar.controller.houtai.employeemanage;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.model.dao.EmployeeDao;
import com.tr.rentcar.model.bean.Employee;

@WebServlet("/houtaiemployeeManage")
public class EmployeeManageController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			EmployeeDao ed = new EmployeeDao();
			// 获取号码
			String tel = req.getParameter("tel");
			List<Employee> emList = null;
			if (tel == null) {
				emList = ed.findAll();
			} else {
				String tel2=tel;
				//System.out.println(tel2);
				emList = ed.findByTel(tel,tel2);
			}
			req.setAttribute("emList", emList);
		} catch (SQLException e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
		req.getRequestDispatcher("/WEB-INF/jsp/houtai_employeeManage.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			int id = Integer.parseInt(req.getParameter("eid"));
			String name = req.getParameter("ename");
			String tel = req.getParameter("etel");
			String identity = req.getParameter("eidentity");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date bir = sdf.parse(req.getParameter("ebir"));
			String status = req.getParameter("selectname");
			if (name != null && tel != null && identity != null && bir != null && status != null) {
				//System.out.println(id+"\t"+name+"\t"+identity+"\t"+tel+"\t"+ bir+"\t"+ status);
				new EmployeeDao().updateAll(id, name, identity, tel, bir, status);
			}
			doGet(req, resp);
		} catch (Exception e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
	}

}
